package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.courseware.dietary.Dietary;
import co.fitstart.entity.courseware.exercise.Exercise;
import co.fitstart.entity.courseware.exercise.PlannedSchedule;
import co.fitstart.entity.courseware.fitnessgroup.BatchCourseware;
import co.fitstart.entity.courseware.fitnessgroup.BatchCoursewareType;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.service.CoursewareService;
import co.fitstart.mobile.service.WeeklyPlanService;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 周计划
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/rounds")
public class WeeklyPlanController {
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private CoursewareService coursewareService;
    
    @Autowired
    private WeeklyPlanService weeklyPlanService;
    
    /**
     * 周计划
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/{courseRoundId}/weekly-plans", method = RequestMethod.GET)
    public ModelAndView weeklyPlans(@Secure User user,
                                    @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("course/batch/weekly-plan-list");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            List<BatchCourseware> batchCoursewares = coursewareService.listGeneratedCourseware(courseRound, BatchCoursewareType.WEEKLY);
            modelAndView.addObject("batchCoursewares", batchCoursewares);

            modelAndView.addObject("batchCoursewares", batchCoursewares);
            modelAndView.addObject("courseRoundId", courseRoundId);
        }
        
        return modelAndView;
    }
    
    /**
     * 周计划饮食
     * @param login
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/{courseRoundId}/weekly-plan", method = RequestMethod.GET)
    public ModelAndView target(@Secure User user,
                               @PathVariable("courseRoundId") Long courseRoundId,
                               @RequestParam(value = "week", required = false) Integer week) {
        ModelAndView modelAndView = new ModelAndView("course/batch/weekly-plan-dietary");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            BatchCourseware batchCourseware = null;
            if(week == null) {
                batchCourseware = coursewareService.getBatchCourseware(courseRound, BatchCoursewareType.WEEKLY);
            } else {
                batchCourseware = coursewareService.getBatchCourseware(courseRound, BatchCoursewareType.WEEKLY, week);
            }
            
            if(batchCourseware != null) {
                Dietary dietary = weeklyPlanService.generallyLoadDietary(batchCourseware);
                
                modelAndView.addObject("batchId", batchCourseware.getId());
                modelAndView.addObject("generated", batchCourseware.getEditStatus().isGenerated());
                modelAndView.addObject("dietary", dietary);
                modelAndView.addObject("week", batchCourseware.getSorting());
                modelAndView.addObject("maxWeek", courseRound.getFitnessGroup().getWeeks());
            }

            modelAndView.addObject("courseRoundId", courseRoundId);
        }
        
        return modelAndView;
    }
    
    
    /**
     * 
     * @param login
     * @param courseRoundId
     * @param week
     * @return
     */
    @RequestMapping(value = "/{courseRoundId}/weekly-plan/exercise", method = RequestMethod.GET)
    public ModelAndView exercise(@Secure User user,
                                 @PathVariable("courseRoundId") Long courseRoundId,
                                 @RequestParam(value = "week", required = false) Integer week) {
        ModelAndView modelAndView = new ModelAndView("course/batch/weekly-plan-exercise");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            BatchCourseware batchCourseware = null;
            if(week == null) {
                batchCourseware = coursewareService.getBatchCourseware(courseRound, BatchCoursewareType.WEEKLY);
            } else {
                batchCourseware = coursewareService.getBatchCourseware(courseRound, BatchCoursewareType.WEEKLY, week);
            }
            
            if(batchCourseware != null) {
                Exercise exercise = weeklyPlanService.loadExercise(batchCourseware);
                List<PlannedSchedule> schedules = weeklyPlanService.listPlannedSchedule(batchCourseware);
                
                modelAndView.addObject("batchId", batchCourseware.getId());
                modelAndView.addObject("generated", batchCourseware.getEditStatus().isGenerated());
                modelAndView.addObject("exercise", exercise);
                modelAndView.addObject("schedules", CollectionUtils.constructMapValueFromList(schedules, "day"));
                modelAndView.addObject("courseSchedules", weeklyPlanService.listSchedule(batchCourseware, schedules));
            }

            modelAndView.addObject("courseRoundId", courseRoundId);
            modelAndView.addObject("week", week);
            modelAndView.addObject("maxWeek", courseRound.getCourse().getWeeks());
            
        }
        
        return modelAndView;
    }
    
}
